package org.jeecg.modules.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.ptCases.entity.PtCases;
import org.jeecg.modules.ptCases.service.IPtCasesService;
import org.jeecg.modules.ptSeekmedical.entity.PtSeekmedical;
import org.jeecg.modules.ptSeekmedical.service.IPtSeekmedicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * app对外全部接口
 */
@Slf4j
@RestController
@RequestMapping("/app/api")
public class AppTaskApiContrller {
    //任务
    @Autowired
    private IPtSeekmedicalService ptSeekmedicalService;
    //病例
    @Autowired
    private IPtCasesService ptCasesService;

    /**
     * 列表查询
     *
     * @param ptSeekmedical
     * @return
     */
    //@AutoLog(value = "求医任务-分页列表查询")
    @ApiOperation(value="求医任务-列表查询", notes="求医任务-列表查询")
    @PostMapping (value = "/task_list")
    public Result<List<PtSeekmedical>> queryPageList(PtSeekmedical ptSeekmedical) {
        QueryWrapper<PtSeekmedical> queryWrapper = new QueryWrapper<>();
        List<PtSeekmedical> pageList = ptSeekmedicalService.list( queryWrapper);
        return Result.OK(pageList);
    }
    /**
     *   添加
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "求医任务-添加")
    @ApiOperation(value="求医任务-添加", notes="求医任务-添加")
    @PostMapping(value = "/task_add")
    public Result<String> task_add(@RequestBody JSONObject jsonObject) {
        //病例对象
        JSONArray cases=jsonObject.getJSONArray("cases");
        String userId= jsonObject.getString("userId");
        String userName= jsonObject.getString("userName");
        String taskTitle= jsonObject.getString("taskTitle");
        String sectionofficeId= jsonObject.getString("sectionofficeId");
        String sectionoffice= jsonObject.getString("sectionoffice");
        String taskclassId= jsonObject.getString("taskclassId");
        String taskClass= jsonObject.getString("taskClass");
        String patientName= jsonObject.getString("patientName");
        String patientAge= jsonObject.getString("patientAge");
        String patientSex= jsonObject.getString("patientSex");
        String illnessTime= jsonObject.getString("illnessTime");
        String phone= jsonObject.getString("phone");
        String patientAddress= jsonObject.getString("patientAddress");
        BigDecimal rewardAmount= jsonObject.getBigDecimal("rewardAmount");
        String taskDescribe= jsonObject.getString("taskDescribe");
        String isStay=jsonObject.getString("isStay");
        Double longitude= jsonObject.getDouble("longitude");
        Double latitude= jsonObject.getDouble("latitude");
        String str=JSONObject.toJSONString(cases, SerializerFeature.WriteClassName);
        List<PtCases> list=JSONObject.parseArray(str,PtCases.class);
        PtSeekmedical ptSeekmedical = new PtSeekmedical();
                ptSeekmedical.setUserId(userId);
                ptSeekmedical.setUserName(userName);
                ptSeekmedical.setTaskTitle(taskTitle);
                ptSeekmedical.setSectionofficeId(sectionofficeId);
                ptSeekmedical.setSectionoffice(sectionoffice);
                ptSeekmedical.setTaskclassId(taskclassId);
                ptSeekmedical.setTaskClass(taskClass);
                ptSeekmedical.setPatientName(patientName);
                ptSeekmedical.setPatientAge(patientAge);
                ptSeekmedical.setPatientSex(patientSex);
                ptSeekmedical.setPatientAddress(patientAddress);
                ptSeekmedical.setPhone(phone);
                ptSeekmedical.setIllnessTime(illnessTime);
                ptSeekmedical.setRewardAmount(rewardAmount);
                ptSeekmedical.setTaskDescribe(taskDescribe);
                ptSeekmedical.setLongitude(longitude);
                ptSeekmedical.setLatitude(latitude);
                ptSeekmedical.setIsRecommend("0");
                ptSeekmedical.setTaskViews("0");
                ptSeekmedical.setReceivingStatus("0");
                ptSeekmedical.setIsStay(isStay);
        ptSeekmedicalService.save(ptSeekmedical);
        for(int k=0;k<list.size();k++){
            PtCases ptCases = new PtCases();
            ptCases.setTaskId(ptSeekmedical.getId());
            ptCases.setTaskTitle(ptSeekmedical.getTaskTitle());
            ptCases.setCaseDetails(list.get(k).getCaseDetails());
            ptCases.setDiagnosisTime(list.get(k).getDiagnosisTime());
            ptCases.setCaseImgs(list.get(k).getCaseImgs());
            ptCasesService.save(ptCases);
        }
        return Result.OK("添加成功！");
    }

    /**
     *  求医任务修改
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "求医任务-修改")
    @ApiOperation(value="求医任务-修改", notes="求医任务-修改")
    @PostMapping(value = "/task_edit")
    public Result<String> task_edit(@RequestBody JSONObject jsonObject) {
        String taskId=jsonObject.getString("taskId");
        String taskTitle= jsonObject.getString("taskTitle");
        String patientName= jsonObject.getString("patientName");
        String patientAge= jsonObject.getString("patientAge");
        String patientSex= jsonObject.getString("patientSex");
        String illnessTime= jsonObject.getString("illnessTime");
        String phone= jsonObject.getString("phone");
        String patientAddress= jsonObject.getString("patientAddress");
        String taskDescribe= jsonObject.getString("taskDescribe");
        Double longitude= jsonObject.getDouble("longitude");
        Double latitude= jsonObject.getDouble("latitude");

        PtSeekmedical ptSeekmedical = ptSeekmedicalService.getById(taskId);
        ptSeekmedical.setTaskTitle(taskTitle);
        ptSeekmedical.setPatientName(patientName);
        ptSeekmedical.setPatientAge(patientAge);
        ptSeekmedical.setPatientSex(patientSex);
        ptSeekmedical.setPatientAddress(patientAddress);
        ptSeekmedical.setPhone(phone);
        ptSeekmedical.setIllnessTime(illnessTime);
        ptSeekmedical.setTaskDescribe(taskDescribe);
        ptSeekmedical.setLongitude(longitude);
        ptSeekmedical.setLatitude(latitude);
        ptSeekmedicalService.updateById(ptSeekmedical);
        return Result.OK("编辑成功!");
    }

    /**
     * 列表查询
     *
     * @param jsonObject
     * @return
     */
    //@AutoLog(value = "求医任务-分页列表查询")
    @ApiOperation(value="任务病例-列表查询", notes="任务病例-列表查询")
    @PostMapping (value = "/case_list")
    public Result<List<PtCases>> case_list(@RequestBody JSONObject jsonObject) {
        String taskId= jsonObject.getString("taskId");
        QueryWrapper<PtCases> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id",taskId);
        List<PtCases> pageList = ptCasesService.list( queryWrapper);
        return Result.OK(pageList);
    }


    /**
     *  病例添加
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "病例-添加")
    @ApiOperation(value="病例-添加", notes="病例-添加")
    @PostMapping(value = "/case_add")
    public Result<String> case_add(@RequestBody JSONObject jsonObject) {
        String taskId= jsonObject.getString("taskId");
        String taskTitle= jsonObject.getString("taskTitle");
        String caseDetails= jsonObject.getString("caseDetails");
        String caseImgs= jsonObject.getString("caseImgs");
        Date diagnosisTime=jsonObject.getDate("diagnosisTime");

        PtCases ptCases = new PtCases();
        ptCases.setTaskId(taskId);
        ptCases.setTaskTitle(taskTitle);
        ptCases.setCaseDetails(caseDetails);
        ptCases.setDiagnosisTime(diagnosisTime);
        ptCases.setCaseImgs(caseImgs);
        ptCasesService.save(ptCases);
        return Result.OK("添加成功!");
    }

    /**
     *  病例修改
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "病例-修改")
    @ApiOperation(value="病例-修改", notes="病例-修改")
    @PostMapping(value = "/case_edit")
    public Result<String> case_edit(@RequestBody JSONObject jsonObject) {
        String casesId= jsonObject.getString("casesId");
        String caseDetails= jsonObject.getString("caseDetails");
        String caseImgs= jsonObject.getString("caseImgs");
        Date diagnosisTime=jsonObject.getDate("diagnosisTime");

        PtCases ptCases = ptCasesService.getById(casesId);
        ptCases.setCaseDetails(caseDetails);
        ptCases.setDiagnosisTime(diagnosisTime);
        ptCases.setCaseImgs(caseImgs);
        ptCasesService.updateById(ptCases);
        return Result.OK("编辑成功!");
    }

    /**
     * 列表查询
     *
     * @param jsonObject
     * @return
     */
    //@AutoLog(value = "求医任务-分页列表查询")
    @ApiOperation(value="任务病例-列表查询", notes="任务病例-列表查询")
    @PostMapping (value = "/task_detail")
    public Result<PtSeekmedical> task_detail(@RequestBody JSONObject jsonObject) {
        String taskId= jsonObject.getString("taskId");
        QueryWrapper<PtSeekmedical> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id",taskId);
        PtSeekmedical ptSeekmedical = ptSeekmedicalService.getOne(queryWrapper);
        return Result.OK(ptSeekmedical);
    }
}
